package com.woniuxy.classteacher.service.impl;


import com.woniuxy.classteacher.mapper.VideoMapper;
import com.woniuxy.classteacher.mapper.VideoRemarkMapper;
import com.woniuxy.classteacher.repository.VideoRepository;
import com.woniuxy.classteacher.service.VideoRemarkService;
import com.woniuxy.classteacher.uitility.TimeUtil;
import com.woniuxy.commons.entity.Vedio;
import com.woniuxy.commons.entity.VideoRemark;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author 李英勇
 * @date 2020/7/21 - 17:51
 */
@Service
public class VideoRemarkServiceImpl implements VideoRemarkService {
    @Autowired
    private VideoRemarkMapper videoRemarkMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private VideoRepository videoRepository;

    /**
     * @Author: 李英勇
     * @Description: 查询视频的相关评论
     * @DateTime: 2020/8/5 16:35
     * @Params: [vid, page, limit]
     * @Return java.util.List<com.woniuxy.commons.entity.VideoRemark>
     */
    @Override
    public List<VideoRemark> findRemarkByVid(int vid, int page, int limit) {
        return videoRemarkMapper.findRemarkByVid(vid, page, limit);
    }

    @Override
    public long findRemarkCountByVid(int vid) {
        return videoRemarkMapper.findRemarkCountByVid(vid);
    }

    /**
     * @Author: 李英勇
     * @Description: 增加视频评论
     * @DateTime: 2020/8/5 16:35
     * @Params: [videoRemark, refreshToken]
     * @Return int
     */
    @Override
    public int addRemark(VideoRemark videoRemark, String refreshToken) {
        //在redis中获取用户的role和rid
        //通过refreshToken获取数据
        Map<Object, Object> data = redisTemplate.opsForHash().entries(refreshToken);
        //获取role
        String role = (String) data.get("role");
        //获取rid
        int rid = (int) data.get("rid");
        //获取账号
        String account = (String) data.get("account");

        videoRemark.setRole(role);
        videoRemark.setRid(rid);
        videoRemark.setTime(TimeUtil.getNow());
        videoRemark.setUaccount(account);
        videoRemark.setStatus(1);
        //加入数据库
        videoRemarkMapper.addRemark(videoRemark);
        //改变评论数
        videoMapper.commentsVideoByVid(videoRemark.getVid());
        //获取video
        Vedio video = videoMapper.getVideoByVid(videoRemark.getVid());
        //更新es
        videoRepository.save(video);

        return 1;
    }


}
